<?php
/*
	本插件：$this->model('model')->方法名();
	其他地方调用：model('jy_yuncangshop/model')->方法名();
*/
namespace jy_yuncangshop;
defined('BY_JYA') or exit('error');
class model extends \plugin{
	public function check_level(){
		if($_SESSION['uid']){
			$levelid = pdo_getcolumn('jy_weishop_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'merchid'=>0),'level');
			if($this->setting['showlevels'] && strpos($this->setting['showlevels'],';'.$levelid.';') === false){
				exi('只有授权会员才能采购商品！','error','jy_yuncangshop/member.index/');
			}
		}else{
			exi('只有授权会员才能采购商品！','error','jy_yuncangshop/member.index/');
		}
		if($this->setting['is_merch']){
			$is_merch = pdo_getcolumn('jy_merch_user',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']),'mid');
			if(!$is_merch){
				exi('只有多商户才能采购商品！','error','jy_yuncangshop/member.index/');
			}
		}
		return true;
	}
	public function upbuy($orderid=0){
		$order=pdo_get('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderid));
		if($order['sendtype']==6){
			$ordergoods=pdo_getall('jy_weishop_order_goods',array('orderid'=>$orderid,'uniacid'=>$_SESSION['uniacid']));
			if($this->plugin_exists('jy_merch')){
				$merchid=pdo_getcolumn('jy_merch_user',array('mid'=>$order['mid'],'uniacid'=>$_SESSION['uniacid']),'id');
				foreach($ordergoods as $gv){
					$goods=pdo_get('jy_weishop_goods',array('id'=>$gv['goodsid'],'uniacid'=>$_SESSION['uniacid']));
					$goods['total']=$gv['total'];
					$goods['optionid']=$gv['optionid'];
					$this->addgoods($goods,$merchid);
				}
			}
			foreach($ordergoods as $gv){
				$this->addmygoods($gv,$merchid);
			}
		}
	}
	public function addmygoods($ordergoods=array(),$merchid=0){
		$data=array(
			'orderid'=>$ordergoods['orderid'],
			'goodsid'=>$ordergoods['goodsid'],
			'mid'=>$ordergoods['mid'],
			'total'=>$ordergoods['total'],
			'optionid'=>$ordergoods['optionid'],
			'buyprice'=>$ordergoods['price'],
		);
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		$data['createtime']=time();
		pdo_insert('jy_yuncangshop_mygoods',$data);
		return true;
	}
	public function addgoods($data=array(),$merchid=0){
		$goodsid=intval($data['id']);
		$optionid=intval($data['optionid']);
		$total=intval($data['total']);
		$api_data=json_decode($data['api_data'],true);
		unset($data['id']);
		unset($data['optionid']);
		unset($data['actionurl']);
		$data['costprice']=$data['price'];
		$data['price']=$api_data['price0']?:$data['price'];
		$data['uniacid']=$_SESSION['uniacid'];
		$data['merchid']=$merchid;
		$data['yungoodsid']=$goodsid;
		$data['onepluginname']='jy_yuncangshop';
		$newgoodsid=pdo_getcolumn('jy_weishop_goods',array('yungoodsid'=>$goodsid,'onepluginname'=>'jy_yuncangshop','merchid'=>$merchid,'uniacid'=>$_SESSION['uniacid']),'id');
		if(!$newgoodsid){
			pdo_insert('jy_weishop_goods',$data);
			$newgoodsid=pdo_insertid();
		}else{
			pdo_update('jy_weishop_goods',array('total +='=>$total),array('id'=>$newgoodsid,'uniacid'=>$_SESSION['uniacid']));
		}
		if(!$optionid){
			return true;
		}
		$specs=pdo_getall('jy_weishop_goods_spec',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		if(count($specs)){
			foreach($specs as $sk=>$sv){
				$options=array();
				$spec_item=pdo_getall('jy_weishop_goods_spec_item',array('specid'=>$sv['id'],'goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
				unset($sv['id']);
				$isadd=pdo_getcolumn('jy_weishop_goods_spec',array('title'=>$sv['title'],'goodsid'=>$newgoodsid,'uniacid'=>$_SESSION['uniacid']),'id');
				if(!$isadd){
					$sv['goodsid']=$newgoodsid;
					pdo_insert('jy_weishop_goods_spec',$sv);
					$sv['newid']=pdo_insertid();
				}else{
					$sv['newid']=$isadd;
				}
				if(count($spec_item)){
					foreach($spec_item as $ok=>$ov){
						$spec_item=$ov;
						$isadd=pdo_getcolumn('jy_weishop_goods_spec_item',array('title'=>$sv['title'],'specid'=>$sv['newid'],'goodsid'=>$newgoodsid,'uniacid'=>$_SESSION['uniacid']),'id');
						if(!$isadd){
							continue;
						}else{
							unset($ov['id']);
							$ov['goodsid']=$newgoodsid;
							$ov['specid']=$sv['newid'];
							pdo_insert('jy_weishop_goods_spec_item',$ov);
						}
					}
				}
				
			}
		}
		$option=pdo_getall('jy_weishop_goods_option',array('goodsid'=>$goodsid,'uniacid'=>$_SESSION['uniacid']));
		if(count($option)){
			foreach($option as $ov){
				$oldspecs=explode('_',$ov['specs']);
				$newspecs=array();
				foreach($specs as $sk=>$sp){
						foreach($sp['options'] as $op){
							if($oldspecs[$sk] == $op['id']){
								$newspecs[]=$op['newid'];
								break;
							}
						}
				}
				$ov['specs']=implode('_',$newspecs);
				$isadd=pdo_getcolumn('jy_weishop_goods_option',array('specs'=>$ov['specs'],'goodsid'=>$newgoodsid,'uniacid'=>$_SESSION['uniacid']),'id');
				if($isadd){
					if($optionid==$ov['id']){
						pdo_update('jy_weishop_goods_option',array('num +='=>$total),array('id'=>$isadd,'uniacid'=>$_SESSION['uniacid']));
					}
					continue;
				}
				$ov['goodsid']=$newgoodsid;
				$ov['price']=$api_data['price0'];
				$ov['cost_price']=$ov['price'];
				if($optionid==$ov['id']){
					$ov['num']=$total;
				}else{
					$ov['num']=0;
				}
				unset($ov['id']);
				pdo_insert('jy_weishop_goods_option',$ov);
			}
		}
		return true;
	}
}
?>